package fr.uniscol;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import fr.uniscol.editors.CustomGregorianCalendarPropertyEditor;
import fr.uniscol.model.BaseAnneeScolaire;
import fr.uniscol.model.Calendrier;
import fr.uniscol.service.IAnneeScolaireService;

@Controller
@SessionAttributes({"tmpBaseACreer", "tmpCalendrier"})
public class CreationBaseController extends UniscolMainController {

    private static Logger log = Logger.getLogger(CreationBaseController.class);

    @Autowired
    private IAnneeScolaireService anneeScolaireService;

    @InitBinder
    protected void initBinder(HttpServletRequest request, WebDataBinder binder){
        binder.registerCustomEditor(GregorianCalendar.class, new CustomGregorianCalendarPropertyEditor(new SimpleDateFormat("dd/MM/yyyy")));
    }

    @ModelAttribute("tmpBaseACreer")
    public BaseAnneeScolaire getBaseACreer(){
        return new BaseAnneeScolaire();
    }

    @ModelAttribute("tmpCalendrier")
    public Calendrier getCalendrier(){
        return new Calendrier();
    }

    @RequestMapping("/base.creation")
    public String init(Model model){

        model.addAttribute("etape", "etape1");

        return "creationbase/cadre";
    }

    @RequestMapping(value="/base.creation.etape1", method=RequestMethod.POST)
    public String validEtape1(@ModelAttribute("tmpBaseACreer") BaseAnneeScolaire baseACreer, Model model){

        model.addAttribute("etape", "etape2");

        return "creationbase/cadre";
    }

    @RequestMapping(value="/base.creation.etape2", method=RequestMethod.POST)
    public String validEtape2(@ModelAttribute("tmpBaseACreer") BaseAnneeScolaire baseACreer,
                                                            @ModelAttribute("tmpCalendrier") Calendrier calendrier, Model model, SessionStatus status, HttpSession session){

        // Nouveau calendrier
        baseACreer.setCalendrier(calendrier);
        log.debug("Début: " + calendrier.getDebut().getTime());
        log.debug("Fin: " + calendrier.getDebut().getTime());

        if(baseACreer.getModeCreation() == BaseAnneeScolaire.MODE_CREATION_SIECLE_STS){
            model.addAttribute("etape", "siecle");

        }else if(baseACreer.getModeCreation() == BaseAnneeScolaire.MODE_CREATION_TEXTE){
            model.addAttribute("etape", "texte");

        }else if(baseACreer.getModeCreation() == BaseAnneeScolaire.MODE_CREATION_BASE_VIDE){
            return terminer(baseACreer, model, status, session);
        }

        return "creationbase/cadre";
    }

    public String terminer(@ModelAttribute("tmpBaseACreer") BaseAnneeScolaire baseACreer, Model model, SessionStatus status, HttpSession session){

        // On enregistre
        if(anneeScolaireService.saveAnneeScolaire(baseACreer)){
            alertSuccess("base.creation.ok", "La nouvelle année scolaire a été créee", model);
            status.setComplete();

        }else{
            alertError("base.creation.error", "Echec de la création de l'année scolaire", model);
        }

        model.addAttribute("etape", "termine");

        return "creationbase/cadre";
    }
}
